package com.bangcommunity.bbframe.mq.kafka.serial;

import com.bangcommunity.bbframe.mq.IQtMessager;
import com.bangcommunity.bbframe.serial.kryo.KryoObjectOutput;
import org.apache.kafka.common.serialization.Serializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * Created by tanghc on 17/2/4.
 */
public class QtMessageSerializer implements Serializer<IQtMessager> {
    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {

    }

    @Override
    public byte[] serialize(String topic, IQtMessager data) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
        KryoObjectOutput output = new KryoObjectOutput(outputStream);
        try {
            output.writeObject(data);
            output.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException("serilize qtmessager ioexception", e);
        }
        return outputStream.toByteArray();
    }

    @Override
    public void close() {

    }
}
